<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>千园千变，万班万课</title>
    <meta name="description" content="这是一个 index 页面">
    <meta name="keywords" content="index">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="icon" type="image/png" href="assets/i/favicon.png">
    <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">
    <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
    <link rel="stylesheet" href="assets/css/amazeui.min.css"/>
    <link rel="stylesheet" href="assets/css/admin.css">
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body data-type="generalComponents">
<div class="tpl-portlet-components">
    <div class="portlet-title">
        <ol class="am-breadcrumb">
            <li><a href="#" class="am-icon-home">首页</a></li>
            <li><a href="#">Amaze UI CSS</a></li>
            <li class="am-active">文字列表</li>
        </ol>
        <button class="refresh_btn"><i class="am-icon-refresh"></i></button>
    </div>
    <div class="tpl-block">
        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-2">
                <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                        <button type="button" class="am-btn am-btn-default am-btn-success btn_add"><span
                                class="am-icon-plus"></span> 新增
                        </button>
                        <button type="button" class="am-btn  am-btn-default am-btn-danger " id="batchDelete"><span
                                class="am-icon-trash-o"></span> 批量删除
                        </button>
                    </div>
                </div>
            </div>
            <div class="am-u-sm-12 am-u-md-7">
                <div class="am-form-group line">
                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm',btnWidth:120}" id="type">
                            <option value=" ">所有类别</option>
                            <option value="sz">上肢力量</option>
                            <option value="xz">下肢力量</option>
                            <option value="rr">柔韧性</option>
                            <option value="ph">平衡性</option>
                            <option value="xt">协调性</option>
                            <option value="lm">灵敏性</option>
                        </select>
                    </div>
                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm',btnWidth:100}" id="starVal" style="width: 100px">
                            <option value=" ">所有星级</option>
                            <option value="1">1星</option>
                            <option value="2">2星</option>
                            <option value="3">3星</option>
                            <option value="4">4星</option>
                            <option value="5">5星</option>
                        </select>
                    </div>
                    <div class="am-form-group" id="classV">
                        <label class="am-checkbox-inline">
                            <input type="checkbox" value="大班"> 大班
                        </label>
                        <label class="am-checkbox-inline">
                            <input type="checkbox" value="中班"> 中班
                        </label>
                        <label class="am-checkbox-inline">
                            <input type="checkbox" value="小班"> 小班
                        </label>
                    </div>
                </div>
            </div>
            <div class="am-u-sm-12 am-u-md-3">
                <div class="am-input-group am-input-group-sm">
                    <input type="text" id="search" class="am-form-field" placeholder="请输入关键字">
                    <span class="am-input-group-btn">
                        <button class="am-btn  am-btn-default am-btn-success tpl-am-btn-success am-icon-search"
                                type="button" id="searchBtn"></button>
                    </span>
                </div>
            </div>
        </div>
        <div class="am-g">
            <div class="am-u-sm-12">
                <table class="am-table am-table-striped am-table-hover table-main" id="table">
                    <thead>
                    <tr>
                        <th class="table-check"><input type="checkbox" class="tpl-table-fz-check"></th>
                        <th class="table-id" width="120">ID</th>
                        <th class="table-title">名称</th>
                        <th class="table-type">水平</th>
                        <th class="table-author ">人数</th>
                        <th class="table-author">难度</th>
                        <th class="table-date ">时长</th>
                        <th>班级</th>
                        <th>上肢</th>
                        <th>下肢</th>
                        <th>柔韧</th>
                        <th>平衡</th>
                        <th>协调</th>
                        <th>灵敏</th>
                        <th class="table-set" width="220">操作</th>
                    </tr>
                    </thead>
                    <tbody></tbody>
                </table>
                <div class="am-cf">
                    <div class="am-fr">
                        <div class="am-pagination tpl-pagination" id="pagination"></div>
                    </div>
                </div>
                <hr>
            </div>
        </div>
    </div>
</div>
<div class="am-g tpl-amazeui-form model_panel" id="editPasswordPanel" style="display: none">
    <div class="am-u-sm-12">
        <form class="am-form am-form-horizontal">
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">课程</label>
                <div class="am-u-sm-9 line">
                    <b id="title" class="am-form-label"></b>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">课程封面图</label>
                <div class="am-u-sm-9 line">
                    <div class="img_content">
                        <input type="file" class="img_upload" id="cover_photo">
                        <i class="am-icon-file-image-o"></i>
                        <img src="" alt="">
                    </div>
                    <button class="am-btn am-btn-default  am-btn-xs preview_btn">
                        <i class="am-icon-eye"></i>
                        预览
                    </button>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">教案图片</label>
                <div class="am-u-sm-9 line">
                    <div class="img_content">
                        <input type="file" class="img_upload" id="teachingt_img">
                        <i class="am-icon-file-image-o"></i>
                        <img src="" alt="">
                    </div>
                    <button class="am-btn am-btn-default  am-btn-xs preview_btn">
                        <i class="am-icon-eye"></i>
                        预览
                    </button>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">教案附件</label>
                <div class="am-u-sm-9 line">
                    <button type="button" class="am-btn am-btn-default am-btn-sm file_btn">
                        <i class="am-icon-cloud-upload"></i> 选择要上传的附件
                        <input type="file" class="file_upload">
                    </button>
                    <small id="teachingt_enclosure"></small>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/amazeui.min.js"></script>
<script src="assets/js/layer.js"></script>
<script src="assets/js/jquery.cookie.js"></script>
<script src="assets/js/md5.min.js"></script>
<script src="assets/js/jquery.pagination.js"></script>
<script src="assets/js/jquery.serializejson.min.js"></script>
<script src="assets/js/request.js"></script>
<script src="assets/js/verification.js"></script>
<script src="assets/js/app.js"></script>
</body>
<script>

var page = '1'
var size = '10'
var search = ''
var type = ''
var classV = ''
var val = ''
var total = 0
function getDataList (callback) {
  AjaxRequest({
    url: '/admin/improve_curriculum_list/',
    data: {page, size, search, type, class: classV, val},
    succFun: function (res) {
      var data = res.data
      total = data.count
      var html = ''
      res.data.list.forEach(function (d, i) {
        html += ` <tr>
                    <td><input type="checkbox" value="${d.improve_id}"></td>
                    <td>${d.improve_id}</td>
                    <td><a href="#" class="btn_edit"  data-id="${d.improve_id}">${d.title}</a></td>
                    <td>${d.level}</td>
                    <td>${d.people_num}</td>
                    <td>${d.difficulty}</td>
                    <td>${d.duration}</td>
                    <td>${d.class}</td>
                    <td>${d.sz}</td>
                    <td>${d.xz}</td>
                    <td>${d.rr}</td>
                    <td>${d.ph}</td>
                    <td>${d.xt}</td>
                    <td>${d.lm }</td>
                    <td>
                        <div class="am-btn-toolbar">
                            <div class="am-btn-group am-btn-group-xs">
                                <button class="am-btn am-btn-success am-btn-xs btn_edit" data-id="${d.improve_id}"><span class="am-icon-pencil-square-o"></span> 编辑</button>
                              <button class="am-btn am-btn-secondary am-btn-xs btn_file_edit" data-id="${d.improve_id}">附件修改</button>
                                <button class="am-btn am-btn-default am-btn-xs am-text-danger btn_delete"  data-id="${d.improve_id}" ><span class="am-icon-trash-o"></span> 删除</button>
                            </div>
                        </div>
                    </td>
                </tr>`
      })
      $('#table tbody').html(html)
      callback && callback()
    }
  })
}
function init () {
  getDataList(function () {
    $('#pagination').pagination({
      pageCount: Math.ceil(total / size),
      jump: true,
      coping: true,
      keepShowPN: true,
      prevContent: '«',
      nextContent: '»',
      jumpBtnCls: 'am-btn am-btn-primary',
      activeCls: 'am-active',
      callback: function (api) {
        page = api.getCurrent().toString()
        getDataList()
      }
    });
  })
}
init()
var ids = []
$('table').on('change', 'tbody [type="checkbox"]', function () {
  ids = []
  var $tableTbody=$('table tbody [type="checkbox"]')
  $tableTbody.each(function (i, d) {
    if ($(d).prop('checked')) {
      ids.push($(this).val())
    }
  })
  if($tableTbody.length===ids.length){
    $('table thead [type="checkbox"]').prop('checked',true)
  }else{
    $('table thead [type="checkbox"]').prop('checked',false)
  }
})
$('table thead [type="checkbox"]').change(function () {
  ids = []
  var isChecked = $(this).prop('checked')
  $('table tbody [type="checkbox"]').each(function (i, d) {
    if (isChecked) {
      $(d).prop('checked', true)
      ids.push($(this).val())
    } else {
      $(d).prop('checked', false)
    }
  })
})
//删除
$('body').on('click', '.btn_delete', function () {
  let id = $(this).attr('data-id')
  layer.confirm('确认要删除该条记录吗？', {
    btn: ['确定', '取消'] //按钮
  }, function () {
    AjaxRequest({
      url: '/admin/improve_curriculum_del/',
      data: {id},
      succFun: function (res) {
        layer.msg(res.message, {icon: 1})
        getDataList()
      }
    })
  });
})
//批量删除
$('#batchDelete').click(function () {
  console.log(ids.length)
  if(ids.length==0){
    layer.msg('请选择要删除的内容！',{icon:2,time:1500})
    return false
  }
  layer.confirm('确认要删除选中的内容吗？', {
    btn: ['确定', '取消'] //按钮
  }, function () {
    AjaxRequest({
      url: '/admin/improve_curriculum_del/',
      data: {id:ids},
      succFun: function (res) {
        layer.msg(res.message, {icon: 1})
        getDataList()
      }
    })
  });
})
//修改或新增
$('body').on('click', '.btn_edit,.btn_add', function (e) {
  e.preventDefault()
  let id = $(this).attr('data-id')
  location.href = 'course_update_or_add.html?id=' + id
})
//搜索
$('#searchBtn').click(function (e) {
  e.preventDefault()
  search = $('#search').val().toString()
  type = $('#type').val().toString()
  val = $('#starVal').val().toString()
  page = '1'
  var selectedV = []
  $("#classV  input[type=checkbox]").each(function () {
    if (this.checked) {
      selectedV.push($(this).val())
    }
  });
  classV = selectedV.join()
  init()
})
function getInfo (id, callback) {
  if (id !== 'undefined') {
    $('#two_pwd,#pwd').parents('.am-form-group').hide()
    AjaxRequest({
      url: '/admin/improve_curriculum_info/',
      data: {improve_id: id},
      succFun: function (res) {
        var data = res.data
        $("form").initForm(res.data);
        callback && callback(res.data)
      }
    })
  }
}

//图片、文件上传
$('[type="file"]').on('change', function (e) {//当chooseImage的值改变时，执行此函数
  var file = this.files[0];
  var isImgUpload = $(this).hasClass('img_upload')
  var _this = this;
  var filePath = $(this).val(), //获取到input的value，里面是文件的路径
    fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(),
    src = window.URL.createObjectURL(this.files[0]);//转成可以在本地预览的格式
  //判断图片的文件大小
  const fileSizeLt4 = file.size / 1024 / 1024 > 4;
  if (fileSizeLt4) {
    layer.msg('图片大小不能大于4M', {icon: 2});
    return false;
  }
  // 检查是否是图片
  if (!fileFormat.match(/.png|.jpg|.jpeg/) && isImgUpload) {
    layer.msg('上传错误,文件格式必须为：png/jpg/jpeg', {icon: 2});
    return false;
  } else {
    var index = layer.load(2, {
      shade: [0.1, '#000'] //0.1透明度的白色背景
    });
    var formData = new FormData();
    formData.append('file', file); // 固定格式
    formData.append('flag', 1); // 固定格式
    $(_this).val('')
    AjaxRequest({
      url: '/admin/file_upload/',
      data: formData,
      contentType: false,
      processData: false,
      succFun: function (res) {
        if (isImgUpload) {
          $(_this).attr('data-src', res.data).siblings('img').attr('src', res.data).siblings('i').hide()
          $(_this).parents('.img_content').siblings('.preview_btn').show()
        } else {
          $(_this).parents('button').siblings('small').html(res.data)
        }
        layer.close(index)
      }
    })
  }
});
//上传文件
$('body').on('click', '.btn_file_edit', function (e) {
  e.preventDefault()
  let id = $(this).attr('data-id')
  getInfo(id, function (data) {
    $('#title').html(data.title)
    if( data.teachingt_img!=''){
      $('#teachingt_img').attr('data-src', data.teachingt_img).siblings('img').attr('src', data.teachingt_img).siblings('i').hide()
      $('#teachingt_img').parents('.img_content').siblings('.preview_btn').show()
    }else{
      $('#teachingt_img').attr('data-src', '').siblings('img').attr('src', '').siblings('i').show()
      $('#teachingt_img').parents('.img_content').siblings('.preview_btn').hide()
    }
    if(data.cover_photo!=''){
      $('#cover_photo').attr('data-src', data.cover_photo).siblings('img').attr('src', data.cover_photo).siblings('i').hide()
      $('#cover_photo').parents('.img_content').siblings('.preview_btn').show()
    }else{
      $('#cover_photo').attr('data-src', '').siblings('img').attr('src', '').siblings('i').show()
      $('#cover_photo').parents('.img_content').siblings('.preview_btn').hide()
    }
    $('#teachingt_enclosure').html(data.teachingt_enclosure)
    layer.open({
      type: 1,
      title: '修改密码',
      area: ['600px', '500px'],
      shadeClose: true, //点击遮罩关闭
      btn: ['确定', '取消'],
      content: $('#editPasswordPanel'),
      yes: function () {
        if (Verification()) {
          var formData = {};
          formData.improve_id = id
          formData.teachingt_img = $('#teachingt_img').attr('data-src')
          formData.cover_photo = $('#cover_photo').attr('data-src')
          formData.teachingt_enclosure = $('#teachingt_enclosure').html()
          AjaxRequest({
            url: '/admin/upload_file/',
            data: formData,
            succFun: function (res) {
              layer.msg(res.message, {icon: 1, time: 1500}, function () {
                layer.closeAll()
              })
            }
          })
        }

      }
    });
  })
})
// 图片预览
$('.preview_btn').click(function (e) {
  e.preventDefault()
  var imgUrl=$(this).siblings('.img_content').children().attr('data-src')
  console.log(imgUrl)
  layer.open({
    type: 1,
    title: false,
    closeBtn: 0,
    area: ['85%', '500px'],
    skin: 'layui-layer-nobg', //没有背景色
    shadeClose: true,
    content: '<div ><img src="'+imgUrl+'" style="width:100%"></div>'
  });
})
</script>
</html>